package com.frank.commonutil;

import lombok.Data;

/**
 * 统一返回前端结果类
 */
@Data
public class R<T> {
    private Integer code; // 返回码
    private String msg; // 返回消息
    private T data; // 返回数据

    // 私有构造方法
    private R() {
    }


    private R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> R ok() {
        return new R(ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getMsg());
    }

    public static <T> R<T> ok(T data) {
        return new R(ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getMsg(),
                data);
    }

    public static <T> R error() {
       return new R(ResultEnum.FAILED.getCode(),
               ResultEnum.FAILED.getMsg());
    }

    public static <T> R error(String msg) {
       return new R(ResultEnum.FAILED.getCode(), msg);
    }

}
